﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CStock;
using System.Diagnostics;

namespace EForms
{
    public partial class TreeViewStockBase : TreeView
    {
        private IDataItem dataItem;
        public TreeViewStockBase()
        {
            
        }
        public IDataItem DataItem
        {
            get
            {
                return dataItem;
            }
        }
       
        protected override void OnAfterSelect(System.Windows.Forms.TreeViewEventArgs e)
        {
            
            base.OnAfterSelect(e);


            try
            {
                TreeNodeStock mySelection = (TreeNodeStock) e.Node;
                SetDataClass(mySelection);


            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.Assert(false, "Error selecting node " + exc.Message);
            }
            //MainClass.Instance.View.Cursor = System.Windows.Forms.Cursors.Default;
        }
        private bool SetDataClass(TreeNodeStock mySelectedNode)
        {
            //IDataClass dc = null;
            switch (mySelectedNode.TreeNodeBase)
            {
                //case TreeNodeInfo.PORTFOLIO:
                //    {
                       
                //    }
                case TreeNodeInfo.SHARE:
                    {
                        //request parent portfolio:
                        TreeNodeStock parentNode = mySelectedNode.Parent as TreeNodeStock;
                        if (parentNode != null)
                        {
                            //parentNode.TreeNodeBase
                            //ShareNode
                            IDataItem mySelectedItem = (IDataItem) mySelectedNode.PortfolioOrDataItem;

                            this.dataItem = mySelectedItem;
                            //PortfolioNode (Select also corresponding Portfolio)
                            //TreeNodeStock portfolioNode = (TreeNodeStock) mySelectedNode.PortfolioTreeNode;
                            //this.programContext.TradeEconomy.Portfolio_Selected = (Portfolio)portfolioNode.PortfolioOrDataItem;
                            //this.programContext.TradeEconomy.Portfolio_Selected.DataItem_Selected = mySelectedItem;

                        }
                        break;
                    }
                
                default:
                    {
                        //no tree node info set
                        //Debug.Assert(false,"TreeViewStock-OnAfterSelect -Unknown TreeNodeInfo");
                        break;
                        //throw new Exception("TreeViewStock-OnAfterSelect()-Unknown TreeNodeInfo");
                    }
            }
            return true;

        }
        public static void FillTreeFromProgramContext(IProgramContext myprogramContext, TreeViewStockBase myTreeView)
        {
            try
            {
                if (myprogramContext.TradeEconomy != null)
                {
                    IPortfolios updatePortfolios = (IPortfolios)myprogramContext.TradeEconomy.Portfolios;
                    Debug.Assert(updatePortfolios != null, "Portfolios not set");
                    if (updatePortfolios == null)
                        return;

                    myTreeView.Nodes.Clear();
                    for (int i = 0; i < updatePortfolios.Items.Count; i++)
                    {
                        IPortfolio portf = updatePortfolios[i];
                        if (portf != null)
                        {
                            AddPortfolioNode(portf, myTreeView);
                        }
                    }
                }
                else
                {
                    myTreeView.Nodes.Clear();
                }
                if (myTreeView.Nodes.Count > 0)
                    myTreeView.Nodes[myTreeView.Nodes.Count - 1].ExpandAll();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Assert(false, "Error filling treeview from programContext ", e.Message);
            }
        }
        public static void AddShareNode(IPortfolio portf, IDataClass dc)
        {
            TreeNodeInfo shareNodeInfo = new TreeNodeInfo(TreeNodeInfo.SHARE, dc);

            TreeNodeStock shareNode = new TreeNodeStock(dc.Name, shareNodeInfo);

            shareNode.ImageIndex = 2;
            shareNode.SelectedImageIndex = 2;



        }
        public void UpdatePortfolioNode(IPortfolio portf)
        {
            for (int i = 0; i < this.Nodes.Count; i++)
            {
                TreeNodeStock portfolioNode = this.Nodes[i] as TreeNodeStock;
                if (portfolioNode.Text == portf.Name)
                {
                    portfolioNode.Nodes.Clear();
                    FillPortfolioNode(portfolioNode, portf);
                    return;
                }
            }
        }
        public static void AddPortfolioNode(IPortfolio portf, TreeViewStockBase myTreeView)
        {
            TreeNodeInfo portfolioNodeInfo = new TreeNodeInfo(TreeNodeInfo.PORTFOLIO, portf);

            string portfolioNodeName = portf.Name;

            //			// *** PORTFOLIONODES ***

            TreeNodeStock portfolioNode = new TreeNodeStock(portfolioNodeName, portfolioNodeInfo);

            portfolioNode.ImageIndex = 0;
            portfolioNode.SelectedImageIndex = 0;



            FillPortfolioNode(portfolioNode, portf);

            myTreeView.Nodes.Add(portfolioNode);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="portfolioNode"></param>
        /// <param name="portf"></param>
        public static void FillPortfolioNode(TreeNodeStock portfolioNode, IPortfolio portf)
        {
            try
            {
                if (portfolioNode != null)
                {
                    portfolioNode.Nodes.Clear();
                    if (portf.DataItems != null && portf.DataItems.Count > 0)
                    {
                        for (int i = 0; i < portf.DataItems.Count; i++)
                        {
                            IDataItem dci = (IDataItem)portf.DataItems[i];
                            TreeNodeInfo shareNodeInfo = new TreeNodeInfo(portfolioNode.Text + "." + TreeNodeInfo.SHARE, dci);

                            string locTreeNodeName = dci.Name;
                            TreeNodeStock shareNode = new TreeNodeStock(locTreeNodeName, shareNodeInfo);
                            shareNode.ImageIndex = 2;
                            shareNode.SelectedImageIndex = 2;

                            shareNode.SetParentTreeNodes(portfolioNode, null);
                            portfolioNode.Nodes.Add(shareNode);
                            //UpdateShareNodes(shareNode, dci);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Assert(false, "Error filling portfolio node for : " + portfolioNode.Text + " ;message is " + e.Message);

            }

        }
    }
}
